package com.learn.project2.service.wx.storage;

import com.learn.project2.bean.MarketStorage;
import com.learn.project2.mapper.MarketStorageMapper;
import com.learn.project2.model.vo.wx.storage.StorageCreateVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Date;
import java.util.Enumeration;
import java.util.UUID;

/**
 * @author 岳子昂
 * @version 1.0
 * @description:
 * @date 2022-01-11 11:18
 */
@Service
public class StorageService2Impl implements StorageService2 {

    @Autowired
    MarketStorageMapper storageMapper;

    @Value("${wxfile.maclocation}")
    String macLocation;

    @Value("${wxfile.winlocation}")
    String winLocation;

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-11 11:21
     */
    @Override
    public StorageCreateVO2 upload(MultipartFile multipartFile) {
        // 设置新文件名
        String originalFilename = multipartFile.getOriginalFilename();
        String uuidName = UUID.randomUUID().toString().replaceAll("-", "");
        String newFileName = uuidName +"_"+ originalFilename;

        // 设置文件储存路径
        //静态资源映射mapping location
        String os = System.getProperty("os.name");
        File fileLocation = null;

        // 判断操作系统
        if(os.toLowerCase().startsWith("mac")){
            fileLocation = new File(macLocation, newFileName);
        }else if(os.toLowerCase().startsWith("win")){
            fileLocation = new File(winLocation, newFileName);
        }

        // 若文件路径不存在，就创建路径
        if (!fileLocation.getParentFile().exists()) {
            fileLocation.getParentFile().mkdirs();
        }

        try {
            multipartFile.transferTo(fileLocation);
        } catch (IOException e) {
            e.printStackTrace();
        }


        // 开始创建vo对象
        // 获取ip地址

        String ipHostAddress = null;
        try {
            Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress ip = (InetAddress) addresses.nextElement();
                    if (ip instanceof Inet4Address
                            && !ip.isLoopbackAddress()
                            && !ip.getHostAddress().contains(":")) {
                        ipHostAddress = ip.getHostAddress();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        String key = newFileName;
        String name = originalFilename;
        String type = multipartFile.getContentType();
        int size = (int) multipartFile.getSize();
        String url;
        if(os.toLowerCase().startsWith("win")){
            url = "http://" + ipHostAddress + ":8083/storage/upload/" + newFileName;
        }else {
            url = "http://" + ipHostAddress + ":8083/storage/upload/" + newFileName;
        }
        MarketStorage storage = new MarketStorage();
        storage.setKey(key);
        storage.setName(name);
        storage.setType(type);
        storage.setSize(size);
        storage.setUrl(url);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setDeleted(false);

        storageMapper.insertSelective(storage);

        StorageCreateVO2 vo = new StorageCreateVO2();

        vo.setId(storage.getId());
        vo.setKey (storage.getKey());
        vo.setName (storage.getName());
        vo.setType (storage.getType ());
        vo.setSize (storage.getSize ());
        vo.setUrl (storage.getUrl ());
        vo.setAddTime (storage.getAddTime ());
        vo.setUpdateTime (storage.getUpdateTime ());

        return vo;
    }
}
